package com.xyy.saas.payment.cores.enums;

import com.xyy.saas.payment.cores.constants.Constants;
import com.xyy.saas.payment.cores.param.Pair;
import lombok.Getter;

@Getter
public enum FBankPowerIdEnum {
    /**
     * 支付宝扫码
     */
    ALIPAY_SCAN_CODE(OpenPayChannelEnum.ALIPAY.getChannel(), "31", "0.21", "2016062900190296", "5122", "ALIPAY", ""),
    /**
     * 支付宝条码
     */
    // ALIPAY_BAR_CODE(OpenPayChannelEnum.ALIPAY.getChannel(), "32", "0.21", "2016062900190296", "5122"),
    /**
     * 支付宝服务窗
     */
    ALIPAY_MINI(OpenPayChannelEnum.ALIPAY.getChannel(), "33", "0.21", "2016062900190296", "5122", "ALIPAY", "MINI_PROGRAM"),
    /**
     * 微信扫码
     */
    WX_SCAN_CODE(OpenPayChannelEnum.WX.getChannel(), "131", "0.21", "314", "", "WECHAT", ""),
    /**
     * 微信条码
     */
    WX_BAR_CODE(OpenPayChannelEnum.WX.getChannel(), "130", "0.21","314", "", "WECHAT", ""),
    /**
     * 微信公众号
     */
    WX_OFFICIAL(OpenPayChannelEnum.WX.getChannel(), "132", "0.21","314", "", "WECHAT", "WECHAT_OFFIACCOUNT"),
    /**
     * 微信APP
     */
    WX_APP(OpenPayChannelEnum.WX.getChannel(), "134", "0.21","314", "", "WECHAT", ""),
    /**
     * 微信小程序
     */
    WX_MINI(OpenPayChannelEnum.WX.getChannel(), "135", "0.21","314", "", "WECHAT", "MINI_PROGRAM"),
    /**
     * 支付宝服务窗
     */
    PINGAN_ALIPAY_MINI(OpenPayChannelEnum.ALIPAY.getChannel(), "AlipayJsapi", "", "", "", "", ""),
    /**
     * 微信公众号(线下通道)
     */
    PINGAN_WX_OFFICIAL(OpenPayChannelEnum.WX.getChannel(), "WeixinJsapi", "", "", "", "", ""),
    /**
     * 微信公众号(线上通道)
     */
    PINGAN_WX_ONLIN(OpenPayChannelEnum.WX.getChannel(), "WeixinJsapiOL", "", "", "", "", ""),
    /**
     * 微信小程序(线上通道)
     */
    PINGAN_WX_MINI_ONLINE(OpenPayChannelEnum.WX.getChannel(), "WeixinProgramOL", "", "", "", "", ""),
    /**
     * 微信小程序(线下通道)
     */
    PINGAN_WX_MINI(OpenPayChannelEnum.WX.getChannel(), "WeixinProgram", "", "", "", "", ""),
    /**
     * 平安信用支付
     */
    PINGAN_CREDIT("credit", "CreditPay", "", "", "", "", ""),
    ;

    public static Pair getAppIdInfo(String businessOrderType, FBankPowerIdEnum powerId) {
        if(businessOrderType == null || powerId == null) {
            throw new RuntimeException("Illegal parameter.");
        }

        if(businessOrderType.contains(BusinessTypeEnum.EC.getType())) {
            switch(powerId) {
                case WX_MINI:
                    return Pair.build("wxa86c96565312fa01", "小药药支付平台收银台");
                case WX_OFFICIAL:
                    return Pair.build("wxe4186dac02a3e77e", "小药药支付平台收银台");
                case PINGAN_WX_MINI:
                    return Pair.build("wx017a9507a663b885", "药帮忙APP收银台");
                case PINGAN_WX_MINI_ONLINE:
                    return Pair.build("wx017a9507a663b885", "药帮忙APP收银台");
                case PINGAN_WX_OFFICIAL:
                    return Pair.build("wxd4ad13b88fcf6378", "北京药帮忙公众号");
                case PINGAN_WX_ONLIN:
                    return Pair.build("wxd4ad13b88fcf6378", "北京药帮忙公众号");
            }
        }
        else if(businessOrderType.contains(BusinessTypeEnum.YIKUAIQIAN.getType())) {
            switch(powerId) {
                case WX_MINI:
                    return Pair.build("wx776afedbfae3a228", "荷叶健康丨问诊买药药店送药到家");
                case WX_OFFICIAL:
                    // 后续有公众号业务需要替换
                    return Pair.build("wxc9f8caca1f74f906", "荷叶健康服务号");
            }
        }

        return Pair.build("",  "");
    }

    public static FBankPowerIdEnum getPowerId(String payChannel, String payMode, String channelMct,String scence) {
        if(payChannel.contains(ChannelEnum.ALIPAY.getCode())) {
            return  channelMct != null&& Constants.PINGAN.equals(channelMct) ? PINGAN_ALIPAY_MINI : ALIPAY_MINI;
        }
        else if(payChannel.contains(ChannelEnum.WX.getCode())) {
            if(PayModeEnum.MINI.getMode().equals(payMode)) {
                if (channelMct != null&&Constants.PINGAN.equals(channelMct)) {
                    if (Constants.OFFLINE.equals(scence)) {
                        return PINGAN_WX_MINI;
                    } else {
                        return PINGAN_WX_MINI_ONLINE;
                    }
                } else {
                    return WX_MINI;
                }
            }
            else if(PayModeEnum.PUB.getMode().equals(payMode)) {
                if (channelMct != null&&Constants.PINGAN.equals(channelMct)) {
                    if (Constants.OFFLINE.equals(scence)) {
                        return PINGAN_WX_OFFICIAL;
                    } else {
                        return PINGAN_WX_ONLIN;
                    }
                } else {
                    return WX_OFFICIAL;
                }
            }
            else if(PayModeEnum.H5.getMode().equals(payMode)) {
                if (channelMct != null&&Constants.YOP.equals(channelMct)) {
                    return WX_OFFICIAL;
                }
            }
        }

        throw new UnsupportedOperationException("UnsupportedOperationException, payChannel=" + payChannel + ", payMode=" + payMode);
    }

    public static FBankPowerIdEnum getPowerId(String payChannel, String payMode) {
        return getPowerId(payChannel, payMode, "","");
    }

    public static FBankPowerIdEnum getPowerIdById(String powerId) {
        for(FBankPowerIdEnum item : FBankPowerIdEnum.values()) {
            if(item.getPowerId().equals(powerId)) {
                return item;
            }
        }

        throw new UnsupportedOperationException("UnsupportedOperationException, powerId=" + powerId);
    }

    /**
     * 根据powerId获取支付模式
     * @param powerId
     * @return
     */
    public static String getPayModeByPowerId(String powerId) {
        if(null == powerId || "".equals(powerId.trim())) {
            return "";
        }

        for(FBankPowerIdEnum fBankPowerIdEnum : FBankPowerIdEnum.values()) {
            if(fBankPowerIdEnum.getPowerId().equals(powerId)) {
                switch (fBankPowerIdEnum) {
                    case ALIPAY_MINI:
                    case WX_MINI:
                        return PayModeEnum.MINI.getMode();
                    case WX_OFFICIAL:
                        return PayModeEnum.PUB.getMode();
                }
            }
        }

        return "";
    }

    FBankPowerIdEnum(String channel, String powerId, String rate, String mchntType, String mcc, String yopChannel, String yopPayWay) {
        this.channel = channel;
        this.powerId = powerId;
        this.rate = rate;
        this.mchntType = mchntType;
        this.mcc = mcc;
        this.yopChannel = yopChannel;
        this.yopPayWay = yopPayWay;
    }

    private String channel;
    private String powerId;
    private String rate;
    private String mchntType;
    private String mcc;
    private String yopChannel;
    private String yopPayWay;
}
